package com.shu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shu.dao.SysRoleAuthorityMapper;
import com.shu.dao.SysRoleMapper;
import com.shu.entity.SysRole;
import com.shu.entity.SysRoleAuthority;
import com.shu.entity.SysRoleAuthorityExample;
import com.shu.service.SysRoleAuthorityService;

@Service
public class RoleAuthorityServiceImpl implements SysRoleAuthorityService {
	
	@Autowired
	private SysRoleAuthorityMapper roleAuthorityMapper;
	@Autowired
	private SysRoleMapper roleMapper;
		
	@Override
	public Map<String, List<String>> getALlRoleAuthority() {
		Map<String, List<String>> allRoleAuthorityMap = new HashMap<String, List<String>>();
		List<SysRoleAuthority> allRoleAuthority =  roleAuthorityMapper.selectByExample(new SysRoleAuthorityExample());
		List<String> roleList = null;
		for(SysRoleAuthority roleAuthority : allRoleAuthority) {
			Integer roleId = roleAuthority.getRoleId();
			String url = roleAuthority.getUrl();
			SysRole role = roleMapper.selectByPrimaryKey(roleId);
			if(null != role) {
				if (allRoleAuthorityMap.containsKey(url)) {
					allRoleAuthorityMap.get(url).add(role.getCode());
				}else {
					roleList = new ArrayList<>();
					roleList.add(role.getCode());
					allRoleAuthorityMap.put(url, roleList);
				}
			}
		}
		return allRoleAuthorityMap;
	}

}
